package com.getpebble.android.framework.endpoint;

import android.content.ContentResolver;
import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Analytics.RemoteAppBehavior;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.install.PebbleManifest.ResourceInfo;
import com.getpebble.android.common.framework.install.PutBytesType;
import com.getpebble.android.common.framework.install.app.AppBundle;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.common.model.PblInstalledAppDataModel;
import com.getpebble.android.framework.app.InstalledApp;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.endpoint.PutBytesEndpoint.InstallResult;
import com.getpebble.android.framework.install.app.AppBundleManager;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppInstallMessage;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppInstallMessage.AppInstallResponseType;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage;
import com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage.AppRemoveResult;
import com.getpebble.android.framework.protocol.outbound.PblOutboundAppInstallMessage;
import com.getpebble.android.framework.protocol.outbound.PblOutboundAppInstallMessage.AppInstallCommand;
import com.getpebble.android.framework.protocol.outbound.PblOutboundAppLifecycleMessage;
import com.getpebble.android.jskit.bridge.JsAppInstaller;
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.UnsignedInteger;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public class InstallAppEndpoint extends RequestableEndpoint {
    public static final String TAG = InstallAppEndpoint.class.getSimpleName();
    private AppBundle mAppBundle;
    private boolean mAutoStart = false;
    private final Context mContext;
    private int mCookieIndex = -1;
    private FrameworkState mFrameworkState;
    private Handler mHandler;
    private int mInstallBankNumber = -1;
    List<UnsignedInteger> mInstallCookies = new ArrayList();
    private Runnable mInstallTimeoutRunnable = new Runnable() {
        public void run() {
            InstallAppEndpoint.this.finish(AppInstallResult.ERROR_TIMEOUT);
        }
    };
    private AppInstallCommand mLastCommand;
    private final IEndpointMessageSender mMessageSender;
    private final PutBytesEndpoint mPutBytesEndpoint;
    private Listener mPutBytesEndpointListener = new Listener() {
        public void progressUpdated(UnsignedInteger current, UnsignedInteger max) {
            Trace.debug(InstallAppEndpoint.TAG, "ProgressUpdated");
            float inProgress = current.times(UnsignedInteger.fromIntBits(100)).floatValue() / max.floatValue();
            int percentage = Math.round((((float) (InstallAppEndpoint.this.mPutBytesFileIndex * 100)) + inProgress) / ((float) InstallAppEndpoint.this.mAppBundle.getInstallableComponents().size()));
            if (InstallAppEndpoint.this.mFrameworkState != null) {
                Trace.debug(InstallAppEndpoint.TAG, "Setting install progress to: " + percentage);
                InstallAppEndpoint.this.mFrameworkState.setAppInstallProgress(percentage);
            }
        }

        public void transferComplete(UnsignedInteger cookie) {
            InstallAppEndpoint.this.mInstallCookies.add(cookie);
            if (InstallAppEndpoint.this.mPutBytesFileIndex + 1 >= InstallAppEndpoint.this.mAppBundle.getInstallableComponents().size()) {
                InstallAppEndpoint.this.installNextCookie();
            } else {
                InstallAppEndpoint.this.sendNextFile();
            }
        }

        public void transferFailed(InstallResult installResult) {
            InstallAppEndpoint.this.handlePutBytesError(installResult);
        }

        public void installComplete(UnsignedInteger cookie) {
            if (InstallAppEndpoint.this.mCookieIndex < InstallAppEndpoint.this.mAppBundle.getInstallableComponents().size() - 1) {
                InstallAppEndpoint.this.installNextCookie();
                return;
            }
            InstallAppEndpoint.this.mState = AppInstallState.SENDING_APP_AVAIL;
            InstallAppEndpoint.this.sendAppAvail(InstallAppEndpoint.this.mInstallBankNumber, InstallAppEndpoint.this.mShouldVibrate);
        }

        public void installFailed(InstallResult installResult) {
            InstallAppEndpoint.this.handlePutBytesError(installResult);
        }
    };
    private int mPutBytesFileIndex = -1;
    private boolean mShouldVibrate = false;
    private AppInstallState mState = AppInstallState.NOT_INSTALLING;
    private Runnable mTimeoutRunnable;
    private UUID mUuidToRemove;
    private UUID mUuidToUpgrade;

    static /* synthetic */ class AnonymousClass6 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$common$framework$install$PutBytesType = new int[PutBytesType.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction = new int[EndpointAction.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$endpoint$PutBytesEndpoint$InstallResult = new int[InstallResult.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$EndpointId = new int[EndpointId.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundAppInstallMessage$AppInstallResponseType = new int[AppInstallResponseType.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundAppInstallMessage$AppInstallCommand = new int[AppInstallCommand.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$PutBytesEndpoint$InstallResult[InstallResult.OK.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$PutBytesEndpoint$InstallResult[InstallResult.CANCELLED.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundAppInstallMessage$AppInstallResponseType[AppInstallResponseType.APP_RESULT.ordinal()] = 1;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$EndpointId[EndpointId.APP_INSTALL_MANAGER.ordinal()] = 1;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.ADD_APP.ordinal()] = 1;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.REMOVE_APP.ordinal()] = 2;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundAppInstallMessage$AppInstallCommand[AppInstallCommand.REMOVE_APP.ordinal()] = 1;
            } catch (NoSuchFieldError e7) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundAppInstallMessage$AppInstallCommand[AppInstallCommand.APP_AVAILABLE.ordinal()] = 2;
            } catch (NoSuchFieldError e8) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundAppInstallMessage$AppInstallCommand[AppInstallCommand.UPGRADE_APP.ordinal()] = 3;
            } catch (NoSuchFieldError e9) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$framework$install$PutBytesType[PutBytesType.APP_RESOURCES.ordinal()] = 1;
            } catch (NoSuchFieldError e10) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$framework$install$PutBytesType[PutBytesType.WORKER.ordinal()] = 2;
            } catch (NoSuchFieldError e11) {
            }
            try {
                $SwitchMap$com$getpebble$android$common$framework$install$PutBytesType[PutBytesType.APP.ordinal()] = 3;
            } catch (NoSuchFieldError e12) {
            }
        }
    }

    public enum AppInstallResult {
        SUCCESS(0),
        ERROR_PRF(-1),
        ERROR_INSTALL_IN_PROGRESS(-2),
        ERROR_BANK_FULL(-3),
        ERROR_BUNDLE_DOWNLOAD(-4),
        ERROR_APP_RESOURCE_LOAD(-5),
        ERROR_APP_WORKER_LOAD(-6),
        ERROR_APP_LOAD(-7),
        ERROR_RESOURCE_INSTALL_FAILED(-8),
        ERROR_WORKER_INSTALL_FAILED(-9),
        ERROR_APP_INSTALL_FAILED(-10),
        ERROR_DB_INSERT(-11),
        ERROR_TIMEOUT(-12),
        ERROR_UNKNOWN(-13),
        ERROR_INVALID(-14),
        ERROR_NO_CONNECTED_DEVICE(-15),
        ERROR_CANCELLED(-16),
        ERROR_SDK_INCOMPATIBLE(-17),
        ERROR_APP_AVAIL_FAILED(-18);
        
        private int mValue;

        private AppInstallResult(int value) {
            this.mValue = value;
        }

        public int getValue() {
            return this.mValue;
        }

        public static AppInstallResult fromValue(int value) {
            for (AppInstallResult result : values()) {
                if (result.getValue() == value) {
                    return result;
                }
            }
            return ERROR_UNKNOWN;
        }

        public static AppInstallResult from(InstallResult result) {
            switch (AnonymousClass6.$SwitchMap$com$getpebble$android$framework$endpoint$PutBytesEndpoint$InstallResult[result.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    return SUCCESS;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    return ERROR_CANCELLED;
                default:
                    return ERROR_APP_INSTALL_FAILED;
            }
        }
    }

    public enum AppInstallState {
        NOT_INSTALLING,
        CLEARING_OLD_APP,
        STARTING_INSTALL,
        SENDING_BYTE_DATA,
        INSTALLING,
        SENDING_APP_AVAIL
    }

    public InstallAppEndpoint(Context context, IEndpointMessageSender messageSender, PutBytesEndpoint putBytesEndpoint) {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        } else if (messageSender == null) {
            throw new IllegalArgumentException("'messageSender' cannot be null!");
        } else {
            this.mContext = context;
            this.mMessageSender = messageSender;
            this.mHandler = new Handler(Looper.getMainLooper());
            this.mPutBytesEndpoint = putBytesEndpoint;
        }
    }

    Set<EndpointId> getSupportedEndpoints() {
        return ImmutableSet.of(EndpointId.APP_INSTALL_MANAGER);
    }

    private Context getContext() {
        return this.mContext;
    }

    private IEndpointMessageSender getMessageSender() {
        return this.mMessageSender;
    }

    ContentResolver getContentResolver() {
        return getContext().getContentResolver();
    }

    private PblDevice getDevice() {
        return getMessageSender().getDevice();
    }

    private void finish(AppInstallResult result) {
        Trace.debug(TAG, "Install complete with result: " + result);
        FrameworkState frameworkState = this.mFrameworkState;
        cleanupInstall();
        if (frameworkState != null) {
            frameworkState.setAppInstallResult(result.getValue());
        }
    }

    private AppInstallResult getLoadErrorByType(PutBytesType type) {
        switch (AnonymousClass6.$SwitchMap$com$getpebble$android$common$framework$install$PutBytesType[type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                return AppInstallResult.ERROR_APP_RESOURCE_LOAD;
            case SMTPMessage.RETURN_HDRS /*2*/:
                return AppInstallResult.ERROR_APP_WORKER_LOAD;
            case ListInfo.INDETERMINATE /*3*/:
                return AppInstallResult.ERROR_APP_LOAD;
            default:
                return AppInstallResult.ERROR_UNKNOWN;
        }
    }

    private AppInstallResult getLoadErrorByIndex(int index) {
        if (index <= this.mAppBundle.getInstallableComponents().size() - 1) {
            return getLoadErrorByType((PutBytesType) this.mAppBundle.getInstallableComponents().get(index));
        }
        Trace.error(TAG, "Index is out of bounds!");
        return AppInstallResult.ERROR_UNKNOWN;
    }

    private synchronized void cleanupInstall() {
        this.mState = AppInstallState.NOT_INSTALLING;
        this.mPutBytesEndpoint.unlock(this.mPutBytesEndpointListener);
        this.mPutBytesFileIndex = -1;
        this.mInstallBankNumber = -1;
        this.mInstallCookies = new ArrayList();
        this.mCookieIndex = -1;
        if (this.mAppBundle != null) {
            this.mAppBundle.closeFile();
            this.mAppBundle = null;
        }
        this.mAutoStart = false;
        this.mShouldVibrate = false;
    }

    private void startTimeout(Runnable runnable) {
        this.mTimeoutRunnable = runnable;
        this.mHandler.postDelayed(this.mTimeoutRunnable, 5000);
    }

    private void cancelTimeout() {
        this.mHandler.removeCallbacks(this.mTimeoutRunnable);
        this.mTimeoutRunnable = null;
    }

    synchronized void addApp(Uri pbwUri, boolean allowReinstall, boolean shouldVibrate, boolean autoStart) {
        if (!isErrorAlreadyInstalling()) {
            final Uri uri = pbwUri;
            final boolean z = allowReinstall;
            final boolean z2 = shouldVibrate;
            final boolean z3 = autoStart;
            new PblAsyncTask() {
                public boolean doInBackground() {
                    return InstallAppEndpoint.this.addAppSync(uri, z, z2, z3);
                }

                public void onTaskSuccess() {
                }

                public void onTaskFailed() {
                }
            }.submit();
        }
    }

    boolean isErrorAlreadyInstalling() {
        if (this.mState == AppInstallState.NOT_INSTALLING) {
            return false;
        }
        Trace.debug(TAG, "Already installing");
        if (this.mFrameworkState != null) {
            this.mFrameworkState.setAppInstallResult(AppInstallResult.ERROR_INSTALL_IN_PROGRESS.getValue());
        }
        return true;
    }

    synchronized boolean addAppSync(Uri pbwUri, boolean allowReinstall, boolean shouldVibrate, boolean autoStart) {
        boolean z;
        Trace.debug(TAG, "addAppSync() pbwUri = " + pbwUri + " allowReinstall = " + allowReinstall + " shouldVibrate = " + shouldVibrate + " autoStart = " + autoStart);
        this.mAppBundle = (AppBundle) new AppBundleManager(getContext()).fetchBundleFromUri(pbwUri);
        if (this.mAppBundle == null) {
            Trace.error(TAG, "Failed to get App Bundle");
            finish(AppInstallResult.ERROR_BUNDLE_DOWNLOAD);
            z = false;
        } else {
            boolean appInstalled = false;
            if (allowReinstall) {
                this.mInstallBankNumber = PblInstalledAppDataModel.fetchBankForUuid(getContentResolver(), getDevice(), this.mAppBundle.getAppInfo().getUuid());
                appInstalled = true;
            }
            this.mAutoStart = autoStart;
            this.mShouldVibrate = shouldVibrate;
            if (this.mInstallBankNumber < 0) {
                this.mInstallBankNumber = PblInstalledAppDataModel.fetchNextEmptyBank(getContentResolver(), getDevice());
                if (this.mInstallBankNumber < 0) {
                    Trace.debug(TAG, "Unable to install app because banks are full");
                    RemoteAppBehavior.logAppInstallFailedHitRemoteSpaceLimit();
                    finish(AppInstallResult.ERROR_BANK_FULL);
                    z = false;
                }
            }
            Trace.debug(TAG, "Installing to bank: " + this.mInstallBankNumber);
            if (allowReinstall && appInstalled) {
                this.mState = AppInstallState.CLEARING_OLD_APP;
                upgradeApp(this.mAppBundle.getAppInfo().getUuid());
            } else {
                startInstall();
            }
            z = true;
        }
        return z;
    }

    private synchronized void startInstall() {
        this.mState = AppInstallState.STARTING_INSTALL;
        if (this.mAppBundle.hasJsFile()) {
            JsAppInstaller.getInstance(this.mContext).installNewJsAppLocally(this.mAppBundle);
        }
        sendNextFile();
    }

    private synchronized void sendNextFile() {
        Trace.debug(TAG, "Sending byte data");
        this.mState = AppInstallState.SENDING_BYTE_DATA;
        this.mPutBytesFileIndex++;
        if (setupPutBytesEndpoint((PutBytesType) this.mAppBundle.getInstallableComponents().get(this.mPutBytesFileIndex))) {
            Trace.debug(TAG, "Starting transfer");
            this.mPutBytesEndpoint.startTransfer();
        } else {
            Trace.error(TAG, "Failed to setup put bytes endpoint");
            finish(AppInstallResult.ERROR_APP_LOAD);
        }
    }

    private boolean setupPutBytesEndpoint(PutBytesType type) {
        if (this.mPutBytesEndpoint.lock(this.mPutBytesEndpointListener)) {
            ResourceInfo resourceInfo = null;
            switch (AnonymousClass6.$SwitchMap$com$getpebble$android$common$framework$install$PutBytesType[type.ordinal()]) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    resourceInfo = this.mAppBundle.getManifest().getResourceInfo();
                    break;
                case SMTPMessage.RETURN_HDRS /*2*/:
                    resourceInfo = this.mAppBundle.getManifest().getWorker();
                    break;
                case ListInfo.INDETERMINATE /*3*/:
                    resourceInfo = this.mAppBundle.getManifest().getAppInfo();
                    break;
                default:
                    Trace.warning(TAG, "Unhandled PutBytesType: " + type);
                    break;
            }
            if (resourceInfo == null) {
                Trace.debug(TAG, "No data to create PutBytesEndpoint");
                return false;
            }
            String name = resourceInfo.getName();
            try {
                InputStream inputStream = this.mAppBundle.getInputStreamForComponent(name);
                int size = resourceInfo.getSize();
                this.mPutBytesEndpoint.setInstallType(type).setInputStream(inputStream).setName(name).setSize(size).setExpectedCrc(resourceInfo.getCrc()).setAppBank(this.mInstallBankNumber);
                return true;
            } catch (IOException e) {
                Trace.error(TAG, "Failed to open app bundle", e);
                return false;
            }
        }
        Trace.error(TAG, "Failed to set put bytes endpoint listener");
        return false;
    }

    private synchronized void installNextCookie() {
        this.mState = AppInstallState.INSTALLING;
        this.mCookieIndex++;
        if (this.mCookieIndex >= this.mInstallCookies.size()) {
            Trace.debug(TAG, "Missing cookie!");
            finish(getLoadErrorByIndex(this.mCookieIndex));
        } else {
            this.mPutBytesEndpoint.install((UnsignedInteger) this.mInstallCookies.get(this.mCookieIndex));
        }
    }

    private void sendAppAvail(int bankNumber, boolean shouldVibrate) {
        getMessageSender().sendMessage(PblOutboundAppInstallMessage.createAppAvailable(bankNumber, shouldVibrate));
        this.mLastCommand = AppInstallCommand.APP_AVAILABLE;
        startTimeout(this.mInstallTimeoutRunnable);
    }

    private void upgradeApp(UUID uuidToUpgrade) {
        getMessageSender().sendMessage(PblOutboundAppInstallMessage.createUpgradeApp(uuidToUpgrade));
        this.mUuidToUpgrade = uuidToUpgrade;
        this.mLastCommand = AppInstallCommand.UPGRADE_APP;
        startTimeout(new Runnable() {
            public void run() {
                InstallAppEndpoint.this.mFrameworkState.setAppUpgradeResult(false);
            }
        });
    }

    private void addInstalledAppToDB() {
        InstalledApp installedApp = InstalledApp.fromAppBundle(this.mAppBundle, this.mInstallBankNumber, getDevice().getAddress());
        try {
            PblInstalledAppDataModel.insertApp(getContentResolver(), installedApp.toContentValues());
            if (this.mAutoStart) {
                startInstalledApp(installedApp.getUuid());
                return;
            }
            RemoteAppBehavior.logAppInstalled(installedApp.getUuid().toString());
            finish(AppInstallResult.SUCCESS);
        } catch (SQLiteException e) {
            Trace.error(TAG, "Got exception while trying to insert app.", e);
            finish(AppInstallResult.ERROR_DB_INSERT);
        }
    }

    private void startInstalledApp(UUID uuid) {
        getMessageSender().sendMessage(PblOutboundAppLifecycleMessage.createStartMessage(uuid));
        RemoteAppBehavior.logAppInstalled(uuid.toString());
        finish(AppInstallResult.SUCCESS);
    }

    private void removeAppFromDB(UUID uuid) {
        Trace.debug(TAG, "removeAppFromDB: " + PblInstalledAppDataModel.removeApp(getContentResolver(), getDevice(), uuid) + " rows removed");
    }

    private void removeApp(UUID uuidToRemove) {
        getMessageSender().sendMessage(PblOutboundAppInstallMessage.createRemoveApp(uuidToRemove));
        this.mUuidToRemove = uuidToRemove;
        this.mLastCommand = AppInstallCommand.REMOVE_APP;
        startTimeout(new Runnable() {
            public void run() {
                Trace.error(InstallAppEndpoint.TAG, "Failed to remove app due to: timeout");
                InstallAppEndpoint.this.mFrameworkState.setAppRemoveResult(AppRemoveResult.TIMEOUT.getId());
            }
        });
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private synchronized void handleAppResult(com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage r8) {
        /*
        r7 = this;
        monitor-enter(r7);
        r7.cancelTimeout();	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.framework.endpoint.InstallAppEndpoint.AnonymousClass6.$SwitchMap$com$getpebble$android$framework$protocol$outbound$PblOutboundAppInstallMessage$AppInstallCommand;	 Catch:{ all -> 0x0048 }
        r5 = r7.mLastCommand;	 Catch:{ all -> 0x0048 }
        r5 = r5.ordinal();	 Catch:{ all -> 0x0048 }
        r4 = r4[r5];	 Catch:{ all -> 0x0048 }
        switch(r4) {
            case 1: goto L_0x0016;
            case 2: goto L_0x006f;
            case 3: goto L_0x00bc;
            default: goto L_0x0011;
        };	 Catch:{ all -> 0x0048 }
    L_0x0011:
        r4 = 0;
        r7.mLastCommand = r4;	 Catch:{ all -> 0x0048 }
    L_0x0014:
        monitor-exit(r7);
        return;
    L_0x0016:
        r2 = r8.getResultAsRemove();	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage.AppRemoveResult.SUCCESS;	 Catch:{ all -> 0x0048 }
        if (r2 == r4) goto L_0x004b;
    L_0x001e:
        r4 = TAG;	 Catch:{ all -> 0x0048 }
        r5 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0048 }
        r5.<init>();	 Catch:{ all -> 0x0048 }
        r6 = "Failed to remove app due to: ";
        r5 = r5.append(r6);	 Catch:{ all -> 0x0048 }
        r6 = r2.toString();	 Catch:{ all -> 0x0048 }
        r5 = r5.append(r6);	 Catch:{ all -> 0x0048 }
        r5 = r5.toString();	 Catch:{ all -> 0x0048 }
        com.getpebble.android.common.core.trace.Trace.error(r4, r5);	 Catch:{ all -> 0x0048 }
    L_0x003a:
        r4 = r7.mFrameworkState;	 Catch:{ all -> 0x0048 }
        if (r4 == 0) goto L_0x0011;
    L_0x003e:
        r4 = r7.mFrameworkState;	 Catch:{ all -> 0x0048 }
        r5 = r2.getId();	 Catch:{ all -> 0x0048 }
        r4.setAppRemoveResult(r5);	 Catch:{ all -> 0x0048 }
        goto L_0x0011;
    L_0x0048:
        r4 = move-exception;
        monitor-exit(r7);
        throw r4;
    L_0x004b:
        r4 = TAG;	 Catch:{ all -> 0x0048 }
        r5 = "Successfully removed app";
        com.getpebble.android.common.core.trace.Trace.debug(r4, r5);	 Catch:{ all -> 0x0048 }
        r4 = r7.mContext;	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.jskit.bridge.JsAppInstaller.getInstance(r4);	 Catch:{ all -> 0x0048 }
        r5 = r7.mUuidToRemove;	 Catch:{ all -> 0x0048 }
        r4.removeOldJsApp(r5);	 Catch:{ all -> 0x0048 }
        r4 = r7.mUuidToRemove;	 Catch:{ all -> 0x0048 }
        r7.removeAppFromDB(r4);	 Catch:{ all -> 0x0048 }
        r4 = r7.mUuidToRemove;	 Catch:{ all -> 0x0048 }
        r4 = r4.toString();	 Catch:{ all -> 0x0048 }
        com.getpebble.android.common.core.trace.Analytics.RemoteAppBehavior.logAppDeleted(r4);	 Catch:{ all -> 0x0048 }
        r4 = 0;
        r7.mUuidToRemove = r4;	 Catch:{ all -> 0x0048 }
        goto L_0x003a;
    L_0x006f:
        r0 = r8.getResultAsAvail();	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage.AppAvailResult.SUCCESS;	 Catch:{ all -> 0x0048 }
        if (r0 == r4) goto L_0x00a4;
    L_0x0077:
        r4 = TAG;	 Catch:{ all -> 0x0048 }
        r5 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0048 }
        r5.<init>();	 Catch:{ all -> 0x0048 }
        r6 = "Failed to make app available: ";
        r5 = r5.append(r6);	 Catch:{ all -> 0x0048 }
        r6 = r0.toString();	 Catch:{ all -> 0x0048 }
        r5 = r5.append(r6);	 Catch:{ all -> 0x0048 }
        r5 = r5.toString();	 Catch:{ all -> 0x0048 }
        com.getpebble.android.common.core.trace.Trace.error(r4, r5);	 Catch:{ all -> 0x0048 }
        r1 = com.getpebble.android.framework.endpoint.InstallAppEndpoint.AppInstallResult.ERROR_APP_AVAIL_FAILED;	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage.AppAvailResult.SDK_INCOMPATIBLE;	 Catch:{ all -> 0x0048 }
        r4 = r0.equals(r4);	 Catch:{ all -> 0x0048 }
        if (r4 == 0) goto L_0x009f;
    L_0x009d:
        r1 = com.getpebble.android.framework.endpoint.InstallAppEndpoint.AppInstallResult.ERROR_SDK_INCOMPATIBLE;	 Catch:{ all -> 0x0048 }
    L_0x009f:
        r7.finish(r1);	 Catch:{ all -> 0x0048 }
        goto L_0x0014;
    L_0x00a4:
        r4 = r7.mFrameworkState;	 Catch:{ all -> 0x0048 }
        if (r4 == 0) goto L_0x0011;
    L_0x00a8:
        r4 = r7.mState;	 Catch:{ all -> 0x0048 }
        r5 = com.getpebble.android.framework.endpoint.InstallAppEndpoint.AppInstallState.SENDING_APP_AVAIL;	 Catch:{ all -> 0x0048 }
        if (r4 != r5) goto L_0x00b3;
    L_0x00ae:
        r7.addInstalledAppToDB();	 Catch:{ all -> 0x0048 }
        goto L_0x0011;
    L_0x00b3:
        r4 = TAG;	 Catch:{ all -> 0x0048 }
        r5 = "Unexpected APP_AVAIL result";
        com.getpebble.android.common.core.trace.Trace.warning(r4, r5);	 Catch:{ all -> 0x0048 }
        goto L_0x0011;
    L_0x00bc:
        r3 = r8.getResultAsUpgrade();	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage.AppUpgradeResult.SUCCESS;	 Catch:{ all -> 0x0048 }
        if (r3 == r4) goto L_0x00ef;
    L_0x00c4:
        r4 = TAG;	 Catch:{ all -> 0x0048 }
        r5 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0048 }
        r5.<init>();	 Catch:{ all -> 0x0048 }
        r6 = "Failed to upgrade app: ";
        r5 = r5.append(r6);	 Catch:{ all -> 0x0048 }
        r6 = r3.toString();	 Catch:{ all -> 0x0048 }
        r5 = r5.append(r6);	 Catch:{ all -> 0x0048 }
        r5 = r5.toString();	 Catch:{ all -> 0x0048 }
        com.getpebble.android.common.core.trace.Trace.error(r4, r5);	 Catch:{ all -> 0x0048 }
    L_0x00e0:
        r4 = r7.mFrameworkState;	 Catch:{ all -> 0x0048 }
        if (r4 == 0) goto L_0x0011;
    L_0x00e4:
        r4 = r7.mState;	 Catch:{ all -> 0x0048 }
        r5 = com.getpebble.android.framework.endpoint.InstallAppEndpoint.AppInstallState.CLEARING_OLD_APP;	 Catch:{ all -> 0x0048 }
        if (r4 != r5) goto L_0x00ff;
    L_0x00ea:
        r7.startInstall();	 Catch:{ all -> 0x0048 }
        goto L_0x0011;
    L_0x00ef:
        r4 = TAG;	 Catch:{ all -> 0x0048 }
        r5 = "Successfully upgraded app";
        com.getpebble.android.common.core.trace.Trace.debug(r4, r5);	 Catch:{ all -> 0x0048 }
        r4 = r7.mUuidToUpgrade;	 Catch:{ all -> 0x0048 }
        r7.removeAppFromDB(r4);	 Catch:{ all -> 0x0048 }
        r4 = 0;
        r7.mUuidToUpgrade = r4;	 Catch:{ all -> 0x0048 }
        goto L_0x00e0;
    L_0x00ff:
        r5 = r7.mFrameworkState;	 Catch:{ all -> 0x0048 }
        r4 = com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage.AppUpgradeResult.SUCCESS;	 Catch:{ all -> 0x0048 }
        if (r3 != r4) goto L_0x010b;
    L_0x0105:
        r4 = 1;
    L_0x0106:
        r5.setAppUpgradeResult(r4);	 Catch:{ all -> 0x0048 }
        goto L_0x0011;
    L_0x010b:
        r4 = 0;
        goto L_0x0106;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.getpebble.android.framework.endpoint.InstallAppEndpoint.handleAppResult(com.getpebble.android.framework.protocol.inbound.PblInboundAppResultInstallMessage):void");
    }

    boolean onRequest(EndpointRequest request, FrameworkState frameworkState) {
        Trace.debug(TAG, "Got request: " + request.getAction());
        this.mFrameworkState = frameworkState;
        switch (AnonymousClass6.$SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[request.getAction().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                Uri pbwUri = (Uri) request.getParcelable(EndpointArgumentKeys.URI);
                boolean autoStart = request.getBoolean(EndpointArgumentKeys.AUTO_START_APP);
                addApp(pbwUri, request.getBoolean(EndpointArgumentKeys.ALLOW_REINSTALL), request.getBoolean(EndpointArgumentKeys.VIBRATE), autoStart);
                return true;
            case SMTPMessage.RETURN_HDRS /*2*/:
                removeApp(request.getUuid(EndpointArgumentKeys.UUID));
                return true;
            default:
                Trace.debug(TAG, "Got unexpected request: " + request.getAction());
                return false;
        }
    }

    boolean onPrfRequest(EndpointRequest request, FrameworkState frameworkState) {
        this.mFrameworkState = frameworkState;
        switch (AnonymousClass6.$SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[request.getAction().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                Trace.debug(TAG, "Unable to add app in PRF mode");
                finish(AppInstallResult.ERROR_PRF);
                return true;
            case SMTPMessage.RETURN_HDRS /*2*/:
                if (this.mFrameworkState == null) {
                    return true;
                }
                Trace.debug(TAG, "Unable to remove app in PRF mode");
                this.mFrameworkState.setAppRemoveResult(AppRemoveResult.IN_PRF.getId());
                return true;
            default:
                return false;
        }
    }

    boolean onReceive(ProtocolMessage message) {
        switch (AnonymousClass6.$SwitchMap$com$getpebble$android$framework$protocol$EndpointId[EndpointId.fromCode(message.getEndpointId()).ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                return onReceiveAppInstall(message);
            default:
                return false;
        }
    }

    private boolean onReceiveAppInstall(ProtocolMessage message) {
        PblInboundAppInstallMessage appInstallMessage = PblInboundAppInstallMessage.createMessage(message);
        switch (AnonymousClass6.$SwitchMap$com$getpebble$android$framework$protocol$inbound$PblInboundAppInstallMessage$AppInstallResponseType[appInstallMessage.getResponseType().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                handleAppResult((PblInboundAppResultInstallMessage) appInstallMessage);
                return true;
            default:
                return false;
        }
    }

    private void handlePutBytesError(InstallResult installResult) {
        if (installResult == null) {
            Trace.warning(TAG, "Unknown install result");
            finish(AppInstallResult.ERROR_UNKNOWN);
        } else if (this.mAppBundle == null) {
            Trace.warning(TAG, "App bundle is null");
            finish(AppInstallResult.from(installResult));
        } else if (this.mAppBundle.getInstallableComponents() == null) {
            Trace.warning(TAG, "InstallableComponents are null");
            finish(AppInstallResult.from(installResult));
        } else {
            finish(getLoadErrorByType((PutBytesType) this.mAppBundle.getInstallableComponents().get(this.mPutBytesFileIndex)));
        }
    }
}
